import json
from pyhanlp import *
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from domain.default_news import news_after_treat_data


def get_news_list():
    engine = create_engine('sqlite:///database/news.db')
    Session = sessionmaker(bind=engine)
    session = Session()
    news_list = session.query(news_after_treat_data).all()
    return_list = []
    for new in news_list:
        list = json.loads(new.json)
        json_text = ','.join(list)
        item = {"id": new.id, "feature": json_text}
        return_list.append(item)
    session.close()
    return return_list


def start_hanlp_cluster(content_list, output_path, cluster_num):
    ClusterAnalyzer = JClass('com.hankcs.hanlp.mining.cluster.ClusterAnalyzer')
    analyzer = ClusterAnalyzer()
    for item in content_list:
        analyzer.addDocument(item["id"], item["feature"])
    result = analyzer.repeatedBisection(cluster_num).toString()
    file = open(output_path, 'w', encoding='utf-8')
    file.write(result)
    file.close()


if __name__ == '__main__':
    content_list = get_news_list()
    start_hanlp_cluster(content_list, './data/results1.txt', 19)